import {
    Text, View, ScrollView, StyleSheet, TouchableOpacity, Platform, Image, FlatList, SafeAreaView,
    Linking, ToastAndroid, TextInput, KeyboardAvoidingView, Alert, Dimensions
} from "react-native";

import AliyunOSS from 'aliyun-oss-react-native'
import HttpUtils from '../../utils/HttpUtils'
import HttpUrls from '../../utils/HttpUrls'
import MD5 from "react-native-md5"
import Constant from '../../utils/Constant'//全局变量

const isIos = Platform.OS == 'ios';

const endPoint = "https://oss-cn-hangzhou.aliyuncs.com"
const bucket = "scim"
const domain = "https://scim.shuaiche.com/"

export default class ImUpload {

    static ossTypeImage = 0;
    static ossTypeVideo = 1;
    static ossTypeSound = 2;
    static ossTypeFile = 3;

    /**
     * 
     * @param {*} fpath 
     * @param {*} successFun 
     * @param {*} ossType 0：图片，1：视频，2：语音，3：文件
     */
    static getAlipz = (fpath, ossType, successFun) => {
        // 获取凭证接口。
        HttpUtils.getRequest(HttpUrls.resourceAccess, {}).then((res) => {
            if (res.success === true) {
                let data = res.data;
                console.log('fpath====', '======================' + fpath)
                if (this.ossTypeImage === ossType) {
                    this.uploadImage(data, fpath, successFun);
                } else if (this.ossTypeVideo === ossType) {
                    this.uploadVideo(data, fpath, successFun);
                } else {

                }

            }
        })
    }

    static getPathPre = (ossType) => {
        let prefix = ''
        if (!Constant.release) {
            prefix = "temp/"
        }

        if (ossType === this.ossTypeImage || ossType === this.ossTypeVideo) {
            prefix += "image/"
        } else if (type === this.ossTypeSound) {
            prefix += "sound/"
        } else if (type === this.ossTypeFile) {
            prefix += "file/"
        }
        return prefix;
    }

    /**
     * 基于 图片上传
     * @param url 请求URL
     * @param params 请求参数
     * @returns {Promise}
     */
    static uploadImage = (data, fpath, successFun) => {
        if (!isIos) {
            let temp = fpath.replace('file://', '')
            fpath = temp
        }
        AliyunOSS.initWithSecurityToken(data.securityToken, data.accessKeyId, data.accessKeySecret, endPoint);
        let prefix = this.getPathPre(this.ossTypeImage);
        let timestamp = Date.parse(new Date()).toString();
        let bojkey = prefix + MD5.hex_md5(timestamp + getRandom(10000, 100000)) + '.jpeg';
        console.log('fpath====', fpath)
        setTimeout(() => {
            AliyunOSS.asyncUpload(bucket, bojkey, fpath).then((res) => {
                if (res === 'UploadSuccess' || res.indexOf("x-oss-request-id") != -1) {
                    // 图片上传成功后， 解析图片。
                    let picUrl = domain + bojkey;
                    console.log('picurl=', picUrl)
                    successFun(picUrl);
                }
            }).catch((error) => {
                console.log(error)
            })
        }, 500);
    }

    /**
    * 基于 视频上传
    * @param url 请求URL
    * @param params 请求参数
    * @returns {Promise}
    */
    static uploadVideo = (data, fpath, successFun) => {
        if (!isIos) {
            let temp = fpath.replace('file://', '')
            fpath = temp
        }
        AliyunOSS.initWithSecurityToken(data.securityToken, data.accessKeyId, data.accessKeySecret, endPoint);
        let prefix = this.getPathPre(this.ossTypeVideo);
        let timestamp = Date.parse(new Date()).toString();
        let bojkey = prefix + MD5.hex_md5(timestamp + getRandom(10000, 100000)) + '.mp4';
        console.log('fpath====', fpath)
        setTimeout(() => {
            AliyunOSS.asyncUpload(bucket, bojkey, fpath).then((res) => {
                if (res === 'UploadSuccess' || res.indexOf("x-oss-request-id") != -1) {
                    // 图片上传成功后， 解析图片。
                    let picUrl = domain + bojkey;
                    console.log('picurl=', picUrl)
                    successFun(picUrl);
                }
            }).catch((error) => {
                console.log(error)
            })
        }, 500);
    }

    /**
    * 基于 语音上传
    * @param url 请求URL
    * @param params 请求参数
    * @returns {Promise}
    */
    static uploadSound = (data, fpath, successFun) => {
        if (!isIos) {
            let temp = fpath.replace('file://', '')
            fpath = temp
        }
        AliyunOSS.initWithSecurityToken(data.securityToken, data.accessKeyId, data.accessKeySecret, endPoint);
        let prefix = this.getPathPre(this.ossTypeSound);
        let timestamp = Date.parse(new Date()).toString();
        let bojkey = prefix + MD5.hex_md5(timestamp + getRandom(10000, 100000)) + '.mp3';
        console.log('fpath====', fpath)
        setTimeout(() => {
            AliyunOSS.asyncUpload(bucket, bojkey, fpath).then((res) => {
                if (res === 'UploadSuccess' || res.indexOf("x-oss-request-id") != -1) {
                    // 图片上传成功后， 解析图片。
                    let picUrl = domain + bojkey;
                    console.log('picurl=', picUrl)
                    successFun(picUrl);
                }
            }).catch((error) => {
                console.log(error)
            })
        }, 500);
    }
}

function getRandom(start, end) {
    var length = end - start;
    var num = parseInt(Math.random() * (length) + start);
    return num;
}



